//
//  LZMusicListView.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/12/7.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMusicListView.h"
#import "LZMusicListTableViewCell.h"
#import <MediaPlayer/MediaPlayer.h>
#import "MPMediaItem+LZAdd.h"

#import "Masonry.h"

@interface LZMusicListView() <UITableViewDelegate, UITableViewDataSource>

@end

@implementation LZMusicListView

- (id)init
{
    self = [super init];
    if (self) {
        [self initialize];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    
    [self configView];
    
    [self addAutoLayout];
}

- (void)configView {
    
    self.backgroundColor = UIColorFromRGB(0x000000, 1);
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.closeButton];
    [self addSubview:self.tableView];
    [self addSubview:self.nextButton];
}

- (void)addAutoLayout {
    
    WS(weakSelf);
    GET_SCREEN_SCALE(scale);
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    
    [self.closeButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.leading.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];
    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(-144);
    }];
    
    [self.nextButton makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(86.5, 86.5));
        make.bottom.mas_equalTo(-57.5*scale);
    }];
}

- (void)closeButton:(id)sender {
    if (self.closeButtonActionBlock) {
        self.closeButtonActionBlock(sender);
    }
}

- (void)nextButton:(id)sender {

    if (self.nextButtonActionBlock) {
        self.nextButtonActionBlock(sender);
    }
}

- (void)showView
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    WS(weakSelf);
    
    if(self.dataArray.count > 0) {
        [self.tableView reloadData];
    }
    
    [UIView animateWithDuration:0.35 animations:^{
        
        weakSelf.alpha = 0.9f;
        
        CGRect frame    = weakSelf.frame;
        frame.origin.y  = 0;
        weakSelf.frame = frame;
        
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hideView
{
    WS(weakSelf);
    
    [UIView animateWithDuration:0.35 animations:^{
        
        weakSelf.alpha = 0.0f;
        
        CGRect frame    = weakSelf.frame;
        frame.origin.y  = SCREEN_HEIGHT;
        weakSelf.frame = frame;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
}

#pragma
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LZMusicListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
    if (!cell) {
        cell = [[LZMusicListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                        reuseIdentifier:@"CellIdentifier"];
    }
    
    MPMediaItem *song = self.dataArray[indexPath.section];
    cell.musicTitle.text = [NSString stringWithFormat:@"%@", song.title];
    cell.musicArtist.text = [NSString stringWithFormat:@"%@", song.artist];
    
    MPMediaItemArtwork *artwork = [song valueForProperty: MPMediaItemPropertyArtwork];
    UIImage *artworkImage = [artwork imageWithSize:cell.musicImageView.bounds.size];
    if (artworkImage) {
        cell.musicImageView.image = artworkImage;
    }
    else {
        cell.musicImageView.image = [UIImage imageNamed: @"noArtwork.png"];
    }
    
    DLog(@"%@, %ld", song.isSelect, (long)indexPath.section);
    
    if ([song.isSelect boolValue] == YES) {
        cell.selectImageView.hidden = NO;
    }
    else {
        cell.selectImageView.hidden = YES;
    }

    return cell;
}

#pragma
#pragma mark UITableViewDelegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if (section == self.dataArray.count-1) {
        return 50.0f;
    }
    
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 85.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(self.didSelectRowBlock) {
        self.didSelectRowBlock(indexPath);
    }
}

- (UILabel *)titleLabel {
    
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = @"Audio list";
        _titleLabel.font = [UIFont systemFontOfSize:18];
        _titleLabel.textColor = UIColorFromRGB(0xffffff, 1);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _titleLabel;
}

- (UIButton *)closeButton {
    
    if (_closeButton == nil) {
        _closeButton = [[UIButton alloc] init];
        [_closeButton setImage:[UIImage imageNamed:@"lz_musiclist_close"] forState:UIControlStateNormal];
        [_closeButton addTarget:self action:@selector(closeButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _closeButton;
}

- (UITableView *)tableView {

    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
    }
    
    return _tableView;
}

- (UIButton *)nextButton {

    if (_nextButton == nil) {
        _nextButton = [[UIButton alloc] init];
        [_nextButton setImage:[UIImage imageNamed:@"lz_musiclist_nextimage"] forState:UIControlStateNormal];
        [_nextButton addTarget:self action:@selector(nextButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _nextButton;
}

@end
